package IA;

import iterators.MenuByPriceItr;
import iterators.MenuByRatingItr;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Christopher
 */
public class Menu{

    public List<MenuItem> getList() {
        return Collections.unmodifiableList(menu);
    }
    private List<MenuItem> menu = new ArrayList<MenuItem>();
    private Double ratingCount;
    private Double ratingTotal;
    
    public Menu(){
        ratingCount = 0.0;
        ratingTotal  = 0.0;
    }
    
    public int Size() {return menu.size();}
    
    public void addItem(String name, String description, Double basePrice,
            Integer dishesAvail, Integer prepTime){
        menu.add(new MenuItem(name, description, basePrice, basePrice,
                dishesAvail, prepTime));
    }
    
    public void addItem(MenuItem item){
        menu.add(item);
    }
    
    public void removeItem(Integer index){
        menu.remove(menu.get(index));
    }
    
    public void rateItem(Integer index, Integer rating){
        ratingCount++;
        ratingTotal += rating;
        
        menu.get(index).setRating(ratingTotal/ratingCount);
        
    }
    
    public void changeItem(Integer index, String name, String description, Double basePrice,
            Integer dishesAvail, Integer prepTime){
        menu.remove(menu.get(index));
        menu.add(index, new MenuItem(name, description, basePrice, basePrice,
                dishesAvail, prepTime));
    }
    
    public void adjustPrepTime(Integer index, Integer prepTime){
        menu.get(index).setPrepTime(prepTime);}
    
    public Menu getMenuByPrice(Double price){
        List<MenuItem> tempMenu = new ArrayList<MenuItem>();
        tempMenu.addAll(menu);
        MenuByPriceItr itr = new MenuByPriceItr(tempMenu
                , price);
        Menu returnMenu = new Menu();
        while(itr.hasNext()){
            returnMenu.addItem(itr.next());
        }
        return returnMenu;
    }
    
    public Menu getMenuByRating(Double rating){
        List<MenuItem> tempMenu = new ArrayList<MenuItem>();
        tempMenu.addAll(menu);
        MenuByRatingItr itr = new MenuByRatingItr(tempMenu
                , rating);
        Menu returnMenu = new Menu();
        while(itr.hasNext()){
            returnMenu.addItem(itr.next());
        }
        return returnMenu;
    }
    
    public void addComment(Integer index, String comment){
        menu.get(index).getCommentList().add(comment);
    }
    
    public List<String> getComment(Integer index){
        return menu.get(index).getCommentList();
    }
    
    public void removeComment(Integer itemIndex, Integer commentIndex){
        menu.get(itemIndex).getCommentList().remove(
                menu.get(itemIndex).getCommentList().get(commentIndex));
    }
    
    @Override
    public String toString(){
        String returnStr = "";
        for(MenuItem item: menu){
            returnStr = returnStr.concat(item.toString()+"/");
        }
        if(!returnStr.isEmpty()){
            return returnStr.substring(0, returnStr.length()-1);
        } else{
            return returnStr;
        }
    }
}
